# ProjectName: impls
# Author: chenmh
# DATE: 2023/10/31 13:53
# DESCRIBE: 线性回归的手动实现+矩阵运算的理解
# VERSION: 1.0

import numpy as np
from utils import RegressionDataSet, RegressionEvaluate

x, w, b, label = RegressionDataSet(num_features=5, num_samples=100).gene_data()
# a = np.ones(shape=(x.shape[0], 1))
# print(x.shape)
# print(w.shape)
# print(b.shape)
# print(label.shape)
print(w)


class LinearModel:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def solve(self):
        raise NotImplementedError


class LinearRegression(LinearModel):
    def solve(self):
        x_b = np.ones(shape=(self.x.shape[0], 1))
        x = np.hstack((self.x, x_b))
        return np.linalg.inv(x.T.dot(x)).dot(x.T).dot(self.y)


linear_model = LinearRegression(x=x, y=label)
w_solve = linear_model.solve()
print(w_solve)
print(w_solve.shape)

reg_eva = RegressionEvaluate(true_value=w, predict_value=w_solve[:-1]).accuracy()
print(reg_eva)
